/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BienInmueble.java
 *
 * Created on 07-abr-2011, 18:22:21
 */

package es.fpsampayo.fin2006;

import es.fpsampayo.fin2006.gui.paneles.BienInmueble;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import es.fpsampayo.recursos.Conexion;
import java.sql.ResultSetMetaData;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author fran
 */
public class PadronForm extends javax.swing.JDialog {

    /** Creates new form BienInmueble */
    private int id;

    private DefaultTableModel modelo;

    public void setId(int id) {
        this.id = id;
    }

    public PadronForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        
        initComponents();
    }

    public PadronForm(java.awt.Frame parent, boolean modal, int id){
        super(parent, modal);
        this.id = id;
        initComponents();
        cargarCampos(id);
        modelo = new DefaultTableModel(){

            @Override
                public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;}

                };
        cargarTitulares(id);

        //btnSiguiente.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnSiguiente = new javax.swing.JButton();
        btnAceptar = new javax.swing.JButton();
        btnAnterior = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        campoParcela = new javax.swing.JTextField();
        campoSgVia = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        campoTitular = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        campoCargo = new javax.swing.JTextField();
        campoCodProvincia = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        campoProvincia = new javax.swing.JTextField();
        campoNomeVia = new javax.swing.JTextField();
        campoCodVia = new javax.swing.JTextField();
        campoConcello = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        campoCodConcello = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaTitulares = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(667, 347));

        btnSiguiente.setText("Siguiente");
        btnSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguienteActionPerformed(evt);
            }
        });

        btnAceptar.setText("Aceptar");
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        btnAnterior.setText("Anterior");
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });

        jLabel9.setText("Sg. Via");

        jLabel1.setText("Parcela");

        jLabel2.setText("Titular");

        jLabel3.setText("Cod. Prov");

        jLabel8.setText("Cod. Concello");

        jLabel5.setText("Cargo");

        jLabel4.setText("Cod. Via");

        jLabel10.setText("Sg. Via");

        jLabel6.setText("Provincia");

        jLabel7.setText("Concello");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(campoCodVia, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(campoCodProvincia, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(18, 18, 18)
                                .addComponent(campoProvincia, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(68, 68, 68)
                                .addComponent(jLabel8)
                                .addGap(18, 18, 18)
                                .addComponent(campoCodConcello, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel7)
                                .addGap(18, 18, 18)
                                .addComponent(campoConcello, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(18, 18, 18)
                                .addComponent(campoSgVia, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel10)
                                .addGap(18, 18, 18)
                                .addComponent(campoNomeVia, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(campoTitular, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(campoParcela, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(campoCargo)))))
                .addContainerGap(13, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(campoCodProvincia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(campoProvincia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(campoCodConcello, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(campoConcello, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(campoCodVia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(campoSgVia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(campoNomeVia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(campoParcela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(campoCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(campoTitular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(170, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {campoParcela, campoTitular});

        jTabbedPane1.addTab("Bien Inmueble", jPanel1);

        tablaTitulares.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tablaTitulares.setCellSelectionEnabled(true);
        jScrollPane1.setViewportView(tablaTitulares);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 642, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Titular", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(426, Short.MAX_VALUE)
                .addComponent(btnAnterior)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSiguiente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAceptar)
                .addContainerGap())
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 667, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar)
                    .addComponent(btnSiguiente)
                    .addComponent(btnAnterior))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguienteActionPerformed
        // TODO add your handling code here:
        //id = id + 1;
        id = id + 1;

        System.out.println("id: " + id);
        cargarCampos(id);
        cargarTitulares(id);
    }//GEN-LAST:event_btnSiguienteActionPerformed

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
        // TODO add your handling code here:
        id = id - 1;

        System.out.println("id: " + id);
        cargarCampos(id);
        cargarTitulares(id);
    }//GEN-LAST:event_btnAnteriorActionPerformed

    /**
    * @param args the command line arguments
    */

    public final void cargarCampos(int id){
        Conexion conexion = new Conexion();
        ResultSet rs = conexion.Select("SELECT * FROM bien_inmueble WHERE id=" + id);
        
        try {
            rs.next();
            campoParcela.setText(rs.getObject("referencia_catastral").toString());
            campoTitular.setText(rs.getObject("nombre").toString());
            campoCodConcello.setText(rs.getObject("cod_municipio_dg").toString());
            campoCodProvincia.setText(rs.getObject("cod_prov").toString());
            campoCodVia.setText(rs.getObject("cod_via").toString());
            campoConcello.setText(rs.getObject("nombre_municipio").toString());
            campoNomeVia.setText(rs.getObject("nombre_via").toString());
            campoProvincia.setText(rs.getObject("nombre_prov").toString());
            campoSgVia.setText(rs.getObject("sg_via").toString());
            System.out.println("Valor rcat :'" + campoParcela.getText() + "'");
            
        } catch (SQLException ex) {
            Logger.getLogger(PadronForm.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            conexion.Desconectar();
        }
    }

    public final void cargarTitulares (int id){
        try {
            String sql = "SELECT * FROM public.titulares WHERE id_bien_inmueble = " + id;
            Conexion conexion = new Conexion();
            ResultSet rs = conexion.Select(sql);
            ResultSetMetaData metadata = rs.getMetaData();
            int ncol = metadata.getColumnCount();
            String campos[] = new String [ncol];
            for ( int i = 0; i < ncol ; i++)
                campos[i] = metadata.getColumnLabel(i+1);
            modelo.setColumnIdentifiers(campos);
            modelo.setRowCount(0);
            while (rs.next()){
                Object[] fila = new Object[ncol];
                for ( int i = 0; i < ncol; i++){
                    try{
                        fila[i] = rs.getObject(i + 1);
                    } catch (SQLException ex) {
                        Logger.getLogger(Listado_GUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                modelo.addRow(fila);
            }
            conexion.Desconectar();

        } catch (SQLException ex) {
            Logger.getLogger(Listado_GUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        tablaTitulares.setModel(modelo);
        tablaTitulares.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    }

    public int cargarCampos(Object nombreCampo, Object valor){
        int error = 0;
        try {
            Conexion conexion = new Conexion();
            ResultSet rs = conexion.Select("SELECT * FROM bien_inmueble WHERE " + nombreCampo + "=" + valor);
            if (rs != null && rs.next()) {
                try {
                    campoParcela.setText(rs.getObject("referencia_catastral").toString());
                    campoTitular.setText(rs.getObject("nombre").toString());
                    campoCodConcello.setText(rs.getObject("cod_municipio_dg").toString());
                    campoCodProvincia.setText(rs.getObject("cod_prov").toString());
                    campoCodVia.setText(rs.getObject("cod_via").toString());
                    campoConcello.setText(rs.getObject("nombre_municipio").toString());
                    campoNomeVia.setText(rs.getObject("nombre_via").toString());
                    campoProvincia.setText(rs.getObject("nombre_prov").toString());
                    campoSgVia.setText(rs.getObject("sg_via").toString());
                } catch (SQLException ex) {
                    Logger.getLogger(PadronForm.class.getName()).log(Level.SEVERE, null, ex);
                } finally{
            conexion.Desconectar();
                }
            }else{
            error = 1;
            }
        } catch (SQLException ex) {
            Logger.getLogger(PadronForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        return error;
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PadronForm dialog = new PadronForm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnSiguiente;
    private javax.swing.JTextField campoCargo;
    private javax.swing.JTextField campoCodConcello;
    private javax.swing.JTextField campoCodProvincia;
    private javax.swing.JTextField campoCodVia;
    private javax.swing.JTextField campoConcello;
    private javax.swing.JTextField campoNomeVia;
    private javax.swing.JTextField campoParcela;
    private javax.swing.JTextField campoProvincia;
    private javax.swing.JTextField campoSgVia;
    private javax.swing.JTextField campoTitular;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tablaTitulares;
    // End of variables declaration//GEN-END:variables

}
